package oa.lyk.service.Personresource;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oa.dao.RewordDao;
import oa.view.RewordView;

public class RewordManager {
	public String query(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String strCurrentPage = request.getParameter("currPage");
		String name=request.getParameter("username");
		if(name != null ){
			try {
				name = new String(name .getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		int currentPage = 1;
		try{
			currentPage=Integer.parseInt(strCurrentPage);
		}catch (Exception e){
			currentPage=1;
		}
		RewordDao rDao = new RewordDao();
		request.setAttribute("Rewordpage", rDao.query(currentPage,name));
		return "queryRewordList";
	}
	
	public String delete(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String id = request.getParameter("id");
		int currPage = Integer.parseInt(request.getParameter("currPage"));
		request.setAttribute("currPage", currPage);
		RewordDao rDao = new RewordDao();
		rDao.del(id);
		return "delReword";
	}
	
	public String now(HttpServletRequest req,HttpServletResponse res){
		return "now";
	}
	
	public String look(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String id = request.getParameter("id");
		String currPage = request.getParameter("currPage");
		RewordDao rDao = new RewordDao();
		RewordView rView = rDao.queryById(id);
		request.setAttribute("rView", rView);
		request.setAttribute("currPage", currPage);
		return "lookReword";
	}
	
	public String update(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String currPage = request.getParameter("currPage");
		String rpid = request.getParameter("rpid");
		String rptype = request.getParameter("rptype");
		String cause = request.getParameter("cause");
		String rtime = request.getParameter("rtime");
		RewordDao rDao = new RewordDao();
		rDao.update(rpid, rptype, cause, rtime);
		request.setAttribute("currPage", currPage);
		return "updateReword";
	}
	
	public String save(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String userid = request.getParameter("userid");
		String deptname = request.getParameter("deptname");
		String rptype = request.getParameter("rptype");
		String cause = request.getParameter("cause");
		String rtime = request.getParameter("rtime");
		RewordDao rDao = new RewordDao();
		rDao.save(userid, rptype, cause, deptname, rtime);
		return "saveReword";
	}
	
}
